package com.nstar.subway.view
{
	import com.nstar.base.util.SystemUtils;
	import com.nstar.subway.mxml.Layer;
	import com.nstar.subway.mxml.Mark;
	
	import flash.events.MouseEvent;
	import flash.system.System;
	
	import mx.containers.Canvas;
	import mx.controls.Alert;
	import mx.controls.Button;
	import mx.controls.CheckBox;
	import mx.controls.TextInput;
	import mx.core.Application;
	import mx.events.FlexEvent;
	import mx.utils.StringUtil;

	public class SubwayMapClass extends Application
	{
		public var cav:Canvas;
		public var btnSave:Button;
		
		public var line1:Layer;
		public var line2:Layer;
		public var line4:Layer;
		public var line5:Layer;
		public var line8:Layer;
		public var line9:Layer;
		public var line10:Layer;
		public var line13:Layer;
		public var lineBT:Layer;
		public var lineKG:Layer;
		
		public var chkLine1:CheckBox;
		public var chkLine2:CheckBox;
		public var chkLine4:CheckBox;
		public var chkLine5:CheckBox;
		public var chkLine8:CheckBox;
		public var chkLine9:CheckBox;
		public var chkLine10:CheckBox;
		public var chkLine13:CheckBox;
		public var chkLineBT:CheckBox;
		public var chkLineKG:CheckBox;

		public var chkLine1text:CheckBox;
		public var chkLine2text:CheckBox;
		public var chkLine4text:CheckBox;
		public var chkLine5text:CheckBox;
		public var chkLine8text:CheckBox;
		public var chkLine9text:CheckBox;
		public var chkLine10text:CheckBox;
		public var chkLine13text:CheckBox;
		public var chkLineBTtext:CheckBox;
		public var chkLineKGtext:CheckBox;

		public var txtSearch:TextInput;
		public var btnSearch:Button;
		public var chkAll:CheckBox;
		
		public function SubwayMapClass()
		{
			super();
			this.addEventListener(FlexEvent.CREATION_COMPLETE,init);
		}
		
		private function init(event:FlexEvent):void{
			btnSave.addEventListener(MouseEvent.CLICK,saveHandler);
			txtSearch.addEventListener(FlexEvent.ENTER,searchHandler);
			btnSearch.addEventListener(MouseEvent.CLICK,searchHandler);
			chkAll.addEventListener(MouseEvent.CLICK,allHandler);
			
		}
		
		private function drawHandler(event:MouseEvent):void{
			var lines:Array = [line1,line2,line4,line5,line8,line9,line10,line13,lineBT,lineKG];
			var points:Array = [23,18,22,23,4,14,21,16,12,4];
			var colors:Array = [0xFF0000,0x0000FF,0x00FFFF,0x80FF80,0x008000,0xBB00BB,
				0xFF00FF,0xFF8080,0xFF0000,0xCB7928];
			
			
			for (var i:int=0;i<lines.length;i++){
				var ary:Array = new Array();
				for(var j:int =0;j<int(points[i]);j++){
					var mark:Mark = new Mark();
					mark.centerX = SystemUtils.randRange(0,480);
					mark.centerY = SystemUtils.randRange(0,380);
					mark.seq = j;
					ary.push(mark);
				}
				if(Layer(lines[i]).url==""){
					Layer(lines[i]).draw(ary,5,colors[i]);
				}
				
			}

		}
		
		private function saveHandler(event:MouseEvent):void{
			var s:String = "";
			for each(var item:Object in cav.getChildren()){
				if(item is Layer){
					s += ">>>"+item.lineInfo.NAME+">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n";
					s += item.save()+"\n";					
				}
			}
			System.setClipboard(s);
			Alert.show("位置信息已经复制到剪切板！","复制数据");
		}
		
		//搜索数据，可搜索站点名称、周边信息
		private function searchHandler(event):void{
			var result:Array = new Array();
			var target:String = StringUtil.trim(txtSearch.text);
			
			var lines:Array = [line1,line2,line4,line5,line8,line9,line10,line13,lineBT,lineKG];
			for each(var line:Layer in lines){
				line.highlight(false);
				var marks:Array = line.marks;
				for each(var mark:Mark in marks){
					var keywords:String = mark.keyWords;
					var stationName:String = mark.stationName;
					var subway:String = mark.subway;
					var bus:String = mark.bus;
					if((keywords && keywords.indexOf(target)!=-1) || 
						(stationName && stationName.indexOf(target)!=-1) ||
						(subway && subway.indexOf(target)!=-1) ||
						(bus && bus.indexOf(target)!=-1)){
							
						result.push(mark);
					}
				}
			}
			
			//处理结果
			for each(var item:Mark in result){
				item.highlight(true);
			}
			
		}
		
		private function allHandler(event:MouseEvent):void{
			var chks:Array = [chkLine1,chkLine2,chkLine4,chkLine5,chkLine8,
				chkLine9,chkLine10,chkLine13,chkLineBT,chkLineKG];
			for each(var item:CheckBox in chks){
				item.dispatchEvent(new MouseEvent(MouseEvent.CLICK));
			}
		}
		
		
	}
}